<?php
/**
 *Users
 * @author tan bing
 * @date 2021-08-10 14:08
 */


namespace Modules\Coupon\Entities;


class UserCoupon extends BaseModel
{

    /**
     * 与模型关联的表名
     *
     * @var string
     */
    protected $table = 'user_coupon';

    /**
     * 重定义主键
     *
     * @var string
     */
    protected $primaryKey = 'user_coupon_id';

    /**
     * 指示是否自动维护时间戳
     *
     * @var bool
     */
    public $timestamps = true;

    /**
     * The storage format of the model's date columns.
     *
     * @var string
     */
    protected $dateFormat = 'Y-m-d H:i:s';

    /**
     * 不可批量赋值的属性。
     *
     * @var array
     */
    protected $guarded = [];

    //可搜索字段
    protected $searchField = ['coupon_name'];

    //可作为条件的字段
    protected $whereField  = [
        'coupon_type', 'is_expire', 'is_use', 'coupon_name', 'coupon_id', 'user_id'
    ];

    // 新增字段
    protected $appends = ['coupon_type_text', 'is_expire_text', 'is_use_text', 'expire_time'];

    /**
     * 获取优惠券类型描述
     * @return string
     * @author tan bing
     * @date 2021-08-13 10:47
     */
    public function getCouponTypeTextAttribute()
    {
        $text = [
            1 => '满减券',
            2 => '折扣券',
        ];

        return $text[$this->coupon_type];
    }

    /**
     * 获取优惠券有效期类型描述
     * @return string
     * @author tan bing
     * @date 2021-08-13 10:47
     */
    public function getIsExpireTextAttribute()
    {
        $text = [
            0 => '未过期',
            1 => '已过期',
        ];

        return $text[$this->is_expire];
    }

    /**
     * 获取优惠券适用范围描述
     * @return string
     * @author tan bing
     * @date 2021-08-13 10:47
     */
    public function getIsUseTextAttribute()
    {
        $text = [
            0 => '未使用',
            1 => '已使用',
        ];

        return $text[$this->is_use];
    }

    /**
     * 获取优惠券到期时间
     * @return false|string
     * @author tan bing
     * @date 2021-09-16 14:24
     */
    public function getExpireTimeAttribute()
    {
        if ($this->expire_type == 20) {
            return date('Y-m-d H:i:s', $this->end_time);
        } elseif ($this->expire_type == 10) {
            return date('Y-m-d H:i:s', (strtotime($this->created_at) + (60 * 60 * 24 * $this->expire_day)));
        }

        return '-';
    }

}